package com.mike.fragmentdemo3.Fragment.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.mike.fragmentdemo3.Fragment.Adpater.MyFragmentPagerAdapter;
import com.mike.fragmentdemo3.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class SmartHomeActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    // UI 对象
    private ImageView mBackHomeIv;
    private ImageView mBack;
    private RadioGroup mTabBarRg;
    private RadioButton mChannelRb;
    private RadioButton mMessageRb;
    private ViewPager mAgerVp;
    private TextView mTimeTv;
    private MyFragmentPagerAdapter mAdapter;

    // 页面常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smart_home_activity);
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        bindViews();
        mChannelRb.setChecked(true);
    }

    private void bindViews() {
        mTabBarRg = findViewById(R.id.rg_tab_bar);
        mChannelRb = findViewById(R.id.rb_channel);
        mMessageRb = findViewById(R.id.rb_message);
        mBack = findViewById(R.id.Iv_upload_air_conditioner);
        mTimeTv = findViewById(R.id.Tv_week);
        mBackHomeIv = findViewById(R.id.backhome_Iv);
        mAgerVp = findViewById(R.id.vpager);
        mAgerVp.setAdapter(mAdapter);
        mAgerVp.setCurrentItem(0);
        mTabBarRg.setOnCheckedChangeListener(this);
        mAgerVp.addOnPageChangeListener(this);

        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 将日期转化为星期
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE", Locale.getDefault());
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String weekDay = sdf.format(calendar.getTime());
        // 获取当前月份
        SimpleDateFormat sdp = new SimpleDateFormat("MMMM", Locale.getDefault());
        sdp.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String month = sdp.format(new Date());
        // 获取当前日期
        SimpleDateFormat sdt = new SimpleDateFormat("dd", Locale.getDefault());
        sdt.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String day = sdt.format(calendar.getTime());
        // 获取当前小时，分钟
        SimpleDateFormat sdh = new SimpleDateFormat("HH:mm", Locale.getDefault());
        sdh.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String hour = sdh.format(calendar.getTime());
        String sTime = weekDay + "       " + month + "   " + day + "       " + hour;
        mTimeTv.setText(sTime);

        mBack.setOnClickListener(v -> finish());
        mBackHomeIv.setOnClickListener(v -> {
            Intent intent = new Intent(SmartHomeActivity.this, MainActivity.class);
            startActivity(intent);
        });

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_channel) {
            mAgerVp.setCurrentItem(PAGE_ONE);
        } else if (checkedId == R.id.rb_message) {
            mAgerVp.setCurrentItem(PAGE_TWO);
        }
    }

    // 重写 ViewPager 页面切换的处理方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // state 的状态有三个，0 表示什么都没做，1 正在滑动，2 滑动完毕
        if (state == 2) {
            switch (mAgerVp.getCurrentItem()) {
                case PAGE_ONE:
                    mChannelRb.setChecked(true);
                    break;
                case PAGE_TWO:
                    mMessageRb.setChecked(true);
                    break;
            }
        }
    }
}
